package managedbean.CRM.SaleOrder;

import entity.CRM.LineItem;
import entity.CRM.SaleQuotation;
import javax.faces.event.ActionEvent;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import session.stateless.CRM.CustomerBeanLocal;
import session.stateless.CRM.SaleOrderBeanLocal;
import session.stateless.CRM.SaleQuotationBeanLocal;
import session.stateless.FRM.CreditAccountBeanLocal;

@ManagedBean(name="addSOWithSQ")
@ViewScoped

public class AddSaleOrderWithQuotationManagerBean implements Serializable {
    
    public AddSaleOrderWithQuotationManagerBean () {
    }
    
    @EJB
    private SaleOrderBeanLocal saleOrderBeanLocal;
    @EJB
    private SaleQuotationBeanLocal saleQuotationBeanLocal;
    @EJB
    private CreditAccountBeanLocal creditAccountBeanLocal;
    @EJB
    private CustomerBeanLocal customerBeanLocal;
    
    private SaleQuotation selectedSaleQuotation;
    private Long saleOrderId;
    private List<LineItem> lineItems;

    private String statusMessage;

    public void initView(PhaseEvent event)
    {        
        if (selectedSaleQuotation == null)                
        {
            selectedSaleQuotation = (SaleQuotation)FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("selectedSaleQuotation");            
        }
        
        try
        {
            List<LineItem> temp_lineItems = new ArrayList<LineItem>();
                for (LineItem temp:selectedSaleQuotation.getLineItems())
                {
                    temp_lineItems.add(temp);                                        
                }
            this.setLineItems(temp_lineItems);     
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
        
    }
    
    public List<LineItem> getLineItems() {
        return lineItems;
    }

    public void setLineItems(List<LineItem> lineItems) {
        this.lineItems = lineItems;
    }

    public SaleOrderBeanLocal getSaleOrderBeanLocal() {
        return saleOrderBeanLocal;
    }

    public void setSaleOrderBeanLocal(SaleOrderBeanLocal saleOrderBeanLocal) {
        this.saleOrderBeanLocal = saleOrderBeanLocal;
    }

    public String getStatusMessage() {
        return statusMessage;
    }

    public void setStatusMessage(String statusMessage) {
        this.statusMessage = statusMessage;
    }

    public SaleQuotation getSelectedSaleQuotation() {
        return selectedSaleQuotation;
    }

    public void setSelectedSaleQuotation(SaleQuotation selectedSaleQuotation) {
        this.selectedSaleQuotation = selectedSaleQuotation;
    }

    public Long getSaleOrderId() {
        return saleOrderId;
    }

    public void setSaleOrderId(Long saleOrderId) {
        this.saleOrderId = saleOrderId;
    }

    public void saveNewSaleOrder(ActionEvent event)
    {
        List<LineItem> temp_lineItems = lineItems;
        
        try
        {                         
            saleOrderId = saleOrderBeanLocal.convertToSaleOrder(selectedSaleQuotation.getSaleLead().getRegistrationNumber(), selectedSaleQuotation.getId(), temp_lineItems);
            statusMessage = "New Sale Order with ID "+saleOrderId+" has been saved successfully.";
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Result: " + statusMessage +")", ""));
        }            
        catch(Exception ex) 
        { 
            ex.printStackTrace(); 
        } 
        
    }
}
